package com.yuba.system.controller.ai;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yuba.common.ai.events.GetVectorDataEvent;
import com.yuba.common.ai.events.UpdateVectorDataEvent;
import com.yuba.common.ai.utils.VectorStoreUtil;
import com.yuba.common.core.domain.R;
import com.yuba.common.core.utils.SpringUtils;
import com.yuba.common.web.core.BaseController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ai/vectorStore")
public class VectorStoreController extends BaseController {

    private final VectorStoreUtil vectorStoreUtil;

    public VectorStoreController(VectorStoreUtil vectorStoreUtil) {
        this.vectorStoreUtil = vectorStoreUtil;
    }

    /**
     * 系统用户同步数据接口
     *
     * @return 无
     */
    @SaCheckPermission("sys:vectorStore:sync")
    @PostMapping("/sync")
    public R<Void> syncVector() {
        // 分类 + 加 内容组成的数组
        GetVectorDataEvent vectorDataEvent = new GetVectorDataEvent();
        SpringUtils.context().publishEvent(vectorDataEvent);
        // 进行同步代码
        boolean saved = vectorStoreUtil.save(vectorDataEvent.getData());
        // 调用comment-ai中的同步函数
        // 触发修改知识库的数据库同步标识
        if (saved) {
            SpringUtils.context().publishEvent(new UpdateVectorDataEvent(vectorDataEvent.getData()));
        }
        return R.ok("同步成功");
    }

}
